package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetMealDTO;
import com.sky.dto.SetMealPageQueryDTO;
import com.sky.entity.SetMeal;
import com.sky.entity.SetMealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.SetMealVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * @description: TODO
 * @author: test
 * @date: 2025/3/28 16:56
 * @version: 1.0
 */
@Service
public class SetMealServiceImpl implements SetMealService {

    private final SetMealMapper setMealMapper;
    private final SetMealDishMapper setMealDishMapper;

    public SetMealServiceImpl(SetMealMapper setMealMapper, SetMealDishMapper setMealDishMapper) {
        this.setMealMapper = setMealMapper;
        this.setMealDishMapper = setMealDishMapper;
    }

    /**
     * 新增套餐
     *
     * @param setMealDTO: 套餐信息
     */
    @Override
    @Transactional
    public void add(SetMealDTO setMealDTO) {
        //保存套餐基本信息
        SetMeal setMeal = SetMeal.builder()
                .image(setMealDTO.getImage())
                .price(setMealDTO.getPrice())
                .name(setMealDTO.getName())
                .categoryId(setMealDTO.getCategoryId())
                .description(setMealDTO.getDescription())
                .status(setMealDTO.getStatus())
                .build();
        setMealMapper.insert(setMeal);
        //保存套餐菜品关系信息
        //将套餐id设置到套餐菜品关系表中
        List<SetMealDish> setMealDishes = setMealDTO.getSetmealDishes();
        setMealDishes.forEach(setMealDish -> setMealDish.setSetMealId(setMeal.getId()));
        setMealDishMapper.insertBatch(setMealDishes);
    }

    /**
     * 套餐条件分页查询
     *
     * @param setMealPageQueryDTO:分页条件
     * @return:
     */
    @Override
    public PageResult queryPageOnConditional(SetMealPageQueryDTO setMealPageQueryDTO) {
        PageHelper.startPage(setMealPageQueryDTO.getPage(), setMealPageQueryDTO.getPageSize());
        Page<SetMealVO> page = setMealMapper.queryPageOnConditional(setMealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除套餐
     *
     * @param ids:要删除的id集合
     */
    @Override
    @Transactional
    public void delete(List<Long> ids) {
        //判断要删除的套餐是否是停售状态
        int rows = setMealMapper.selectByIdAndStatus(ids);
        if (rows > 0) {
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        //批量删除套餐,同时删除套餐和菜品的关联关系
        setMealMapper.deleteBatchByIds(ids);
        setMealDishMapper.deleteBatchBySetMealIds(ids);
    }

    /**
     * 根据id查询套餐
     *
     * @param id: 套餐id
     * @return:
     */
    @Override
    public SetMealVO getById(Long id) {
        return setMealMapper.getById(id);
    }

    /**
     * 修改套餐
     *
     * @param setMealDTO:
     */
    @Override
    @Transactional
    public void update(SetMealDTO setMealDTO) {
        SetMeal setMeal = SetMeal.builder()
                .id(setMealDTO.getId())
                .categoryId(setMealDTO.getCategoryId())
                .name(setMealDTO.getName())
                .price(setMealDTO.getPrice())
                .status(setMealDTO.getStatus())
                .description(setMealDTO.getDescription())
                .image(setMealDTO.getImage())
                .build();

        //修改套餐基本信息
        setMealMapper.update(setMeal);
        //先删除对应套餐的套餐与菜品关系数据,再重新添加
        if (!setMealDTO.getSetmealDishes().isEmpty()) {
            //删除对应套餐的套餐与菜品关系数据
            setMealDishMapper.deleteBatchBySetMealIds(Collections.singletonList(setMeal.getId()));
        }
        //添加新的套餐与菜品关系数据
        if (setMealDTO.getSetmealDishes() != null && !setMealDTO.getSetmealDishes().isEmpty()) {
            //设置套餐id
            setMealDTO.getSetmealDishes().forEach(setMealDish -> setMealDish.setSetMealId(setMeal.getId()));
            //添加套餐与菜品关系数据
            setMealDishMapper.insertBatch(setMealDTO.getSetmealDishes());
        }

    }

    /**
     * 起售停售套餐
     *
     * @param status: 状态
     * @param id:套餐id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        SetMeal setMeal = SetMeal.builder()
                .id(id)
                .status(status).build();
        setMealMapper.update(setMeal);
    }

}